﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SchoolManagementSystem
{
    public partial class Akun : Form
    {
        public BindingSource bindingSource1 = new BindingSource();
        AkunAdd add = null;
        AkunEdit edit = null;
        
        public Akun()
        {
            InitializeComponent();
            Style.setDatagridviewStyle(dataGridView1);
        }

        private void Akun_Load(object sender, EventArgs e)
        {
            this.update();
        }

        void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.RowIndex == -1 && e.ColumnIndex > -1)
            {
                /*
                e.PaintBackground(e.CellBounds, false);

                Rectangle r2 = e.CellBounds;
                r2.Y += e.CellBounds.Height / 2;
                r2.Height = e.CellBounds.Height / 2;
                e.PaintContent(r2);
                e.Handled = true;
                 * */ 
            }
        }

        void dataGridView1_Paint(object sender, PaintEventArgs e)
        {
            /* 
            string title = "Pilihan";

            if (activity == 0)
            {
                for (int i = 0; i < this.dataGridView1.Columns.Count; i++)
                {
                    if (this.dataGridView1.Columns[i].Name == "Edit")
                    {
                        Rectangle r1 = this.dataGridView1.GetCellDisplayRectangle(i, -1, true); //get the column header cell

                        r1.Width = r1.Width * 2;

                        e.Graphics.FillRectangle(new SolidBrush(this.dataGridView1.ColumnHeadersDefaultCellStyle.BackColor), r1);

                        StringFormat format = new StringFormat();
                        format.Alignment = StringAlignment.Center;
                        format.LineAlignment = StringAlignment.Center;

                        e.Graphics.DrawString(title,
                            this.dataGridView1.ColumnHeadersDefaultCellStyle.Font,
                            new SolidBrush(this.dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor),
                            r1,
                            format);
                        i += 2;
                    }
                }
            }
             * */ 
        }

        public void update()
        {
            this.dataGridView1.Columns.Clear();

            bindingSource1.DataSource = Database.getInstance().query("select * from Account");
            this.dataGridView1.DataSource = bindingSource1;

            DataGridViewLinkColumn editColumn = new DataGridViewLinkColumn();
            editColumn.UseColumnTextForLinkValue = true;
            editColumn.Name = "Edit";
            editColumn.Text = "Edit";
            editColumn.HeaderText = "";
            editColumn.ReadOnly = true;
            editColumn.VisitedLinkColor = Color.Blue;
            this.dataGridView1.Columns.Add(editColumn);

            DataGridViewLinkColumn deleteColumn = new DataGridViewLinkColumn();
            deleteColumn.UseColumnTextForLinkValue = true;
            deleteColumn.Name = "Delete";
            deleteColumn.HeaderText = "";
            deleteColumn.Text = "Delete";
            deleteColumn.ReadOnly = true;
            deleteColumn.VisitedLinkColor = Color.Blue;
            this.dataGridView1.Columns.Add(deleteColumn);

            this.dataGridView1.Columns["acc_id"].Visible = false;
            this.dataGridView1.Columns["password"].Visible = false;
            this.dataGridView1.Columns["user_name"].HeaderText = "Akun";
            this.dataGridView1.Columns["acc_name"].HeaderText = "Nama Lengkap";
            this.dataGridView1.Columns["contact"].HeaderText = "No Kontak";
            this.dataGridView1.Columns["type"].HeaderText = "Tipe";

            this.dataGridView1.Refresh();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Edit")
            {
                if (this.edit == null || this.edit.IsDisposed)
                    edit = new AkunEdit(this, 
                        this.dataGridView1.Rows[e.RowIndex].Cells["acc_name"].Value.ToString(),
                        this.dataGridView1.Rows[e.RowIndex].Cells["user_name"].Value.ToString(),
                        this.dataGridView1.Rows[e.RowIndex].Cells["contact"].Value.ToString(),
                        this.dataGridView1.Rows[e.RowIndex].Cells["type"].Value.ToString());

                edit.MdiParent = this.MdiParent;
                edit.Show();
                edit.Activate();
            } 
            else if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Delete")
            {
                var result = MessageBox.Show("Apakah Anda ingin menghapus akun tersebut ?", "Peringatan !", MessageBoxButtons.OKCancel);
                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    DataTable dt = Database.getInstance().tes("select * from Account");
                    DataRow[] data = dt.Select("acc_name='" + this.dataGridView1.Rows[e.RowIndex].Cells["acc_name"].Value.ToString() + "' AND user_name='" + this.dataGridView1.Rows[e.RowIndex].Cells["user_name"].Value.ToString() + "'");

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (dt.Rows[i].ItemArray[0].ToString() == data[0].ItemArray[0].ToString())
                        {
                            dt.Rows[i].Delete();
                            Database.getInstance().updateDB(dt);
                            this.dataGridView1.Rows.RemoveAt(e.RowIndex);
                            break;
                        }
                    }   
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.update();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (add == null || add.IsDisposed)
                add = new AkunAdd(this);

            add.MdiParent = this.MdiParent;
            add.Show();
            add.Activate();
        }
    }
}
